package com.recsmile.sgs.entity;

import com.recsmile.sgs.util.SerializableUtil;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;

/**
 * Created by Administrator on 2016/8/29.
 */
public class Deck {
    public static enum DeckPack {
        STANDARD, EX, BATTLE;
    }
    private static final EnumMap<DeckPack, List<Card>> systemCards;
    static {

        systemCards = new EnumMap<DeckPack, List<Card>>(DeckPack.class);
        for(DeckPack deckPack : DeckPack.values()){
            systemCards.put(deckPack, new ArrayList<>());
        }
        SAXReader saxReader = new SAXReader();
        try {
            ClassLoader classLoader  =  Thread.currentThread().getContextClassLoader();
           Document document = saxReader.read(classLoader.getResourceAsStream("Cards.xml"));
           for(Element element : (List<Element>)document.getRootElement().elements()){
               Card cards = (Card) SerializableUtil.getObjenesis().newInstance(classLoader.loadClass(element.attributeValue("class")));
               cards.read(element);
               systemCards.get(DeckPack.STANDARD).add(cards);
           }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


    }


    private List<Card> usedCards ;
    private List<Card> deckCards ;

    public static Deck createDeck(DeckPack deckPack){
        List<Card> cards = systemCards.get(deckPack);
        Deck deck = new Deck();
        deck.deckCards = new ArrayList<>(cards);
        deck.usedCards = new ArrayList<>();
        return  deck;
    }


    public Card drawCard(){
        return deckCards.get(0);
    }
}
